package com.example.shopping_li.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.shopping_li.R;

public class UpdateHeadDialog extends Dialog implements View.OnClickListener {
    TextView camerTv, localTv, cancelTv;

    public UpdateHeadDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_headimg);
        camerTv = findViewById(R.id.dialog_headimg_camera);
        localTv = findViewById(R.id.dialog_headimg_local);
        cancelTv = findViewById(R.id.dialog_headimg_cancel);
        camerTv.setOnClickListener(this);
        localTv.setOnClickListener(this);
        cancelTv.setOnClickListener(this);
    }

    public interface LocalImageListener {
        public void openLocalImage();

        public void openCamera();

    }

    LocalImageListener localImageListener;

    public void setLocalImageListener(LocalImageListener localImageListener) {
        this.localImageListener = localImageListener;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_headimg_camera:
                if (localImageListener != null) {
                    localImageListener.openCamera();
                }
                break;
            case R.id.dialog_headimg_local:
                if (localImageListener != null) {
                    localImageListener.openLocalImage();
                }
                break;
            case R.id.dialog_headimg_cancel:
                cancel();
                break;
        }
    }

    public void setDialogWidth() {
        Window window = getWindow();//当前屏幕窗口对象
        WindowManager.LayoutParams wlp = window.getAttributes();//获取窗口信息参数
        //获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = (int) d.getWidth();//对话框窗口宽度为屏幕窗口宽度
        wlp.gravity = Gravity.BOTTOM;//从底部弹出对话框
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);
    }
}
